/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年7月16日 上午11:10:34
 */
package com.absir.appserv.system.service.impl;

import java.io.IOException;

import com.absir.appserv.upgrade.UpgradeService;
import com.absir.bean.basis.Base;
import com.absir.bean.inject.value.Bean;
import com.absir.core.kernel.KernelObject;
import com.absir.servlet.InDispathContext;

/**
 * @author absir
 *
 */
@Base
@Bean
public class UpgradeServiceImpl extends UpgradeService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.appserv.upgrade.UpgradeService#stop()
	 */
	@Override
	public Object stop() throws IOException {

		Object context = InDispathContext.getServletContext();
		if (context != null) {
			context = KernelObject.declaredGet(context, "context");
			if (context != null) {
				context = KernelObject.declaredGet(context, "context");
				// tomcat set lock false
				KernelObject.declaredSend(context, "setAntiJARLocking", false);
				KernelObject.declaredSend(context, "setAntiResourceLocking", false);
				KernelObject.declaredSend(context, "setReloadable", false);
				KernelObject.declaredSend(context, "setPrivileged", true);
				final Object ctx = context;
				return new IUpgradeStart() {

					@Override
					public void start() throws Throwable {

						/*
						 * KernelObject.declaredSend(ctx, "setReloadable",
						 * true); String reload =
						 * String.valueOf(System.currentTimeMillis());
						 * ZipOutputStream outputStream = new
						 * ZipOutputStream(HelperFile.openOutputStream(new
						 * File(InDispathContext .getContextResourcePath() +
						 * "WEB-INF/lib/_reload.jar")), KernelCharset.DEFAULT);
						 * ZipEntry zipEntry = new ZipEntry("_reload");
						 * zipEntry.setExtra(reload.getBytes());
						 * outputStream.putNextEntry(zipEntry);
						 * outputStream.flush();
						 * HelperIO.closeQuietly(outputStream);
						 * KernelObject.declaredSend(ctx, "stop");
						 */
						KernelObject.declaredSend(ctx, "reload");
					}
				};
			}
		}

		return super.stop();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.absir.appserv.upgrade.UpgradeService#start()
	 */
	@Override
	public void start(Object stopDone) throws IOException {

		if (stopDone != null) {
			// tomcat reload
			KernelObject.declaredSend(stopDone, "start");
			return;
		}

		Object context = InDispathContext.getServletContext();
		if (context != null) {
			context = KernelObject.declaredGet(context, "context");
			if (context != null) {
				context = KernelObject.declaredGet(context, "context");
				// tomcat reload
				KernelObject.declaredSend(context, "reload");
				return;
			}
		}
	}
}
